<?php

namespace asyncService\processHandles;

use asyncService\lib\ProcessHandle;
use asyncService\mainService;
use asyncService\lib\log;

class subHandle  extends ProcessHandle {

    protected $log ;

    function __construct($worker, $mainService)
    {
        parent::__construct($worker, $mainService);

        $this->log = log::getInstance("subHandle");
    }

    function callback() {

        log::getInstance()->info("i am in sub");

		while($row_message = $this->worker->pop()){

		    $this->log->debug("sub message : {$row_message}");

			$message = json_decode($row_message,true);

			switch ($message[0]){
				case mainService::EVENT_DATA:

                    $this->log->info("  service ready consume !");

					$serivce = $this->mainService->services[$message[1]['p']];

                    $this->log->info(" service consume done !");

					$serivce->consume($message);

					break;
                case mainService::EVENT_STOP:
                    sleep(1);
                    $mypid = getmypid();
                    $topid = $message[1];

                    $this->log->info("I am get stop event,mypid {$mypid} topid {$topid}");

                    if($mypid != $topid){
                        $this->worker->push($row_message);
                    }else{
                        $this->run  = false;
                        $this->worker->exit();
                    }
                    break;
                default:
                    $this->log->info(" get default message!");
                    break;
			}

            $this->log->debug(" go to next! ");
		}

	}

}